package com.qiangqiang.resoldhouse.controller;

import com.qiangqiang.resoldhouse.dto.HouseResourceDTO;
import com.qiangqiang.resoldhouse.dto.NewsDTO;
import com.qiangqiang.resoldhouse.entity.News;
import com.qiangqiang.resoldhouse.entity.NewsSelect;
import com.qiangqiang.resoldhouse.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

/**
 * Description: resoldhouse
 * Create by jeff on 2018-8-14 9:58
 */
@Controller
public class NewsController {
    
    @Autowired
    private NewsService newsService;


    @RequestMapping("/news_list")
    public String getPage(@RequestParam(name = "page", defaultValue = "1") Integer page, Map<String, Object> map, NewsSelect select) {
        if (select.getOrder() == null) select.setOrder("default");
        if (select.getTitle() == null || select.getTitle().trim().equals("")) select.setTitle(null);
        map.put("pageDTO", newsService.findPage(page, select));
        map.put("select", select);
        return "news_list";
    }

    @RequestMapping("/news_detail")
    public String getNewsDetail(@RequestParam("newsId") Long newsId, Map<String, Object> map) {
        map.put("news", newsService.findNewsById(newsId));
        return "news_content";
    }

    @RequestMapping("/news_content")
    public String getNewsContent(@RequestParam("newsId") Long newsId, Map<String, Object> map,NewsSelect select) {
        map.put("news", newsService.findNewsById(newsId));
        if (select.getOrder() == null || select.getOrder().trim().equals("")) select.setOrder("default");
        if (select.getTitle() == null || select.getTitle().trim().equals("")) select.setTitle(null);
        Integer page =1 ;
        map.put("pageDTO", newsService.findPage(page, select));
        map.put("select", select);
        return "news_content";
    }

    
//    @RequestMapping("/news_content")
//    public String newsContent(){
//        return "news_content";
//    }



    //后台经纪人展示页面
    @RequestMapping("/sys_news")
    public String getSysPage(@RequestParam(defaultValue = "1") Integer page, Map<String, Object> map, NewsSelect select) {
        if (select.getOrder() == null || select.getOrder().trim().equals("")) select.setOrder("default");
        if (select.getTitle() == null || select.getTitle().trim().equals("")) select.setTitle(null);
        map.put("pageDTO", newsService.findPage(page, select));
        map.put("select", select);
        return "system/sys_news";
    }

    //后台添加经纪人
    @PostMapping("/insertnews")
    public String insertNews(News news) {
        newsService.insertNews(news);
        return "redirect:/sys_news";
    }


    //后台删除经纪人
    @GetMapping("/sys_deletenews")
    public String delete_sysnews(@RequestParam Long newsId) {
        newsService.deleteNews(newsId);
        return "redirect:/sys_news";
    }


    //后台修改经纪人审核状态
    @GetMapping("/status_sysnews")
    public String status_sysnews(@RequestParam Long newsId, boolean status) {
        System.out.println("con De " +newsId);
        newsService.passNews(newsId, status);
        return "redirect:/sys_news";
    }
}


